javascript complex recurrsion [on hold]

Posted by Achilles on Programmers See other posts from Programmers or by Achilles
Published on 2013-11-06T11:36:40Z Indexed on 2013/11/06 16:10 UTC
Read the original article Hit count: 390

Filed under:
|
|

Given Below is my data in data array. What i am doing in code below is that from that given data i have to construct json in a special format which i also gave below.

//code start here
   var hierarchy={};
    hierarchy.name="Hierarchy";
    hierarchy.children=[{"name":"","children":[{"name":"","children":[]}]}];
    var countryindex;
    var flagExist=false;
    var data = [
        {country :"America", city:"Kansas", employe:'Jacob'},
        {country :"Pakistan", city:"Lahore", employe:'tahir'},
        {country :"Pakistan", city:"Islamabad", employe:'fakhar'} ,
        {country :"Pakistan", city:"Lahore", employe:'bilal'},
        {country :"India", city:"d", employe:'ali'}  ,
        {country :"Pakistan", city:"Karachi", employe:'eden'},
        {country :"America", city:"Kansas", employe:'Jeen'}  ,
        {country :"India", city:"Banglore", employe:'PP'} ,
        {country :"India", city:"Banglore", employe:'JJ'}  ,

    ];


    for(var i=0;i<data.length;i++)
    {


        for(var j=0;j<hierarchy.children.length;j++)
        {
            //for checking country match
            if(hierarchy.children[j].name==data[i].country)
            {
                countryindex=j;
                flagExist=true;
                break;

            }

        }

        if(flagExist)//country match now no need to add new country just add city in it
        {
            var cityindex;
            var cityflag=false;
            //hierarchy.children[countryindex].children.push({"name":data[i].city,"children":[]})
            //if(hierarchy.children[index].children!=undefined)
            for(var k=0;k< hierarchy.children[countryindex].children.length;k++)
            {
                //for checking city match
                if(hierarchy.children[countryindex].children[k].name==data[i].city)
                {
                    // hierarchy.children[countryindex].children[k].children.push({"name":data[i].employe})
                    cityflag=true;
                    cityindex=k;
                    break;
                }

            }
            if(cityflag)//city match now add just empolye at that city index
            {
                hierarchy.children[countryindex].children[cityindex].children.push({"name":data[i].employe});

                cityflag=false;
            }
            else//no city match so add new with employe also as this is new city so its emplye will be 1st
            {
                hierarchy.children[countryindex].children.push({"name":data[i].city,children:[{"name":data[i].employe}]});
                //same as above
                //hierarchy.children[countryindex].children[length-1].children.push({"name":data[i].employe});


            }
            flagExist=false;
        }
        else{ //no country match adding new country //with city also as this is new city of new country
            console.log("sparta");
            hierarchy.children.push({"name":data[i].country,"children":[{"name":data[i].city,"children":[{"name":data[i].employe}]}]});

            // hierarchy.children.children.push({"name":data[i].city,"children":[]});
        }


        //console.log(hierarchy);
    }
    hierarchy.children.shift();
 var j=JSON.stringify(hierarchy);

//code ends here

//here is the json which i seccessfully formed from the code

{

"name":"Hierarchy",
"children":[
{
"name":"America",
"children":[
{
"name":"Kansas",
"children":[{"name":"Jacob"},{"name":"Jeen"}]}]},
{
"name":"Pakistan",
"children":[
{
"name":"Lahore",
"children":
[
{"name":"tahir"},{"name":"bilal"}]},
{
"name":"Islamabad",
"children":[{"name":"fakhar"}]},
{
"name":"Karachi",
"children":[{"name":"eden"}]}]},
{
"name":"India",
"children":
[
{
"name":"d",
"children":
[
{"name":"ali"}]},
{
"name":"Banglore",
"children":[{"name":"PP"},{"name":"JJ"}]}]}]}

Now the orignal problem is that currently i am solving this problem for data of array of three keys and i have to go for 3 nested loops now i want to optimize this solution so that if data array of object has more than 3 key say 5 {country :"America", state:"NewYork",city:"newYOrk",street:"elm", employe:'Jacob'}, or more than my solution will not work and i cannot decide before how many keys will come so i thought recursion may suit best here. But i am horrible in writing recurrsion and the case is also complex. Can some awesome programmer help me writing recurrsion or suggest some other solution.

© Programmers or respective owner

Related posts about JavaScript

Related posts about JSON